package controllers;

import play.mvc.*;
import play.data.*;
import static play.data.Form.*;
import views.html.pageErreurConnexion;
import views.html.ConsoleAdmin.*;

import models.*;

/**
 * Manage a database of matieres
 */
public abstract class AdminMatiere extends Controller {

	/**
	 * This result directly redirect to application home.
	 */
	public final static Result GO_HOME = redirect(routes.AdminMatiere
			.listMatiere(0, "id_matiere", "asc", ""));

	/**
	 * Handle default path requests, redirect to matieres list
	 */
	public static Result indexMatiere() {
		if (Application.estAdmin()) {
			return GO_HOME;
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Display the paginated list of matieres.
	 * 
	 * @param page
	 *            Current page number (starts from 0)
	 * @param sortBy
	 *            Column to be sorted
	 * @param order
	 *            Sort order (either asc or desc)
	 * @param filter
	 *            Filter applied on matiere names
	 */
	public static Result listMatiere(final int page, final String sortBy,
			final String order, final String filter) {
		if (Application.estAdmin()) {
			return ok(listMatiere.render(
					Matiere.page(page, 10, sortBy, order, filter), sortBy,
					order, filter));
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Display the 'edit form' of a existing Matiere.
	 * 
	 * @param id
	 *            Id of the matiere to edit
	 */
	public static Result editMatiere(final Long id) {
		if (Application.estAdmin()) {
			final Form<Matiere> matiereForm = form(Matiere.class).fill(
					Matiere.findById(id));
			return ok(editMatiere.render(id, matiereForm));
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Handle the 'edit form' submission
	 * 
	 * @param id
	 *            Id of the matiere to edit
	 */
	public static Result updateMatiere(final Long id) {
		if (Application.estAdmin()) {
			final Form<Matiere> matiereForm = form(Matiere.class)
					.bindFromRequest();
			if (matiereForm.hasErrors()) {
				return badRequest(editMatiere.render(id, matiereForm));
			}

			matiereForm.get().update(id);
			flash("success", "Matiere " + matiereForm.get().getLibelleM()
					+ " has been updated");
			return GO_HOME;
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Display the 'new matiere form'.
	 */
	public static Result createMatiere() {
		if (Application.estAdmin()) {
			final Form<Matiere> matiereForm = form(Matiere.class);
			return ok(createMatiere.render(matiereForm));
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Handle the 'new matiere form' submission
	 */
	public static Result saveMatiere() {
		if (Application.estAdmin()) {
			final Form<Matiere> matiereForm = form(Matiere.class)
					.bindFromRequest();
			if (matiereForm.hasErrors()) {
				return badRequest(createMatiere.render(matiereForm));
			}
			matiereForm.get().save();
			flash("success", "Matiere " + matiereForm.get().getLibelleM()
					+ " has been created");
			return GO_HOME;
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Handle matiere deletion
	 */
	public static Result deleteMatiere(final Long id) {
		if (Application.estAdmin()) {
			Matiere.delete(id);
			flash("success", "Matiere has been deleted");
			return GO_HOME;
		} else
			return ok(pageErreurConnexion.render());
	}

}
